package top.ligongzi.hrm.service.impl;

import top.ligongzi.hrm.config.Oauth2ClientDetailsProperties;
import top.ligongzi.hrm.contstants.AuthConstants;
import top.ligongzi.hrm.domain.LoginUser;
import top.ligongzi.hrm.exception.ValidUtils;
import top.ligongzi.hrm.mapper.LoginUserMapper;
import top.ligongzi.hrm.service.ILoginUserService;
import top.ligongzi.hrm.util.HttpUtil;
import top.ligongzi.hrm.vo.Oauth2ClientDetails;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import net.bytebuddy.asm.Advice;
import org.apache.http.auth.AUTH;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yaosang
 * @since 2021-01-31
 */
@Service
public class LoginUserServiceImpl extends ServiceImpl<LoginUserMapper, LoginUser> implements ILoginUserService {

    @Autowired
    private LoginUserMapper loginUserMapper;
    @Override
    public boolean insert(LoginUser user) {
        //保存loginUser
        loginUserMapper.insert(user);
        //保存Meal中间表

        loginUserMapper.saveUserMeal(user.getId(),user.getMealId());

        return true;
    }

    @Autowired
    private Oauth2ClientDetailsProperties oauth2ClientDetailsProperties;
    @Override
    public Map<String, String> login(LoginUser loginUser) {
        ValidUtils.assertNotNull(loginUser.getUsername(), "请输入用户名或密码！");
        ValidUtils.assertNotNull(loginUser.getPassword(), "请输入用户名或密码！");
        ValidUtils.assertNotNull(loginUser.getType(), "请使用正确方式来访问！");
        //========写死版本=========//
//        String clientId = null;
//        String clientSecret = null;

//        if (loginUser.getType().intValue()== AuthConstants.LOGIN_TYPE_ADMIN){
//             clientId = "admin";
//             clientSecret = "1";
//        }else{
//            clientId = "website";
//            clientSecret = "1";
//        }

        Oauth2ClientDetails clientDetials = oauth2ClientDetailsProperties.getClientDetials(loginUser.getType());
        ValidUtils.assertNotNull(clientDetials, "请使用正确方式来访问！");

        //替换地址
        String url = String.format(AuthConstants.PASSWORD_AUTH_URL, clientDetials.getClientId(),
                clientDetials.getClientSecret(),
                loginUser.getUsername(),loginUser.getPassword());

        //发起请求并返回
        return HttpUtil.sendPost(url);

    }

    @Override
    public Map<String, String> refreshToken(Map<String, String> params) {
        String refreshToken = params.get("refreshToken");
        String typeStr = params.get("type");
        ValidUtils.assertNotNull(refreshToken, "请传入刷新token！");
        ValidUtils.assertNotNull(typeStr, "请传入刷新token！");

        Integer type = Integer.valueOf(typeStr);
        Oauth2ClientDetails clientDetials = oauth2ClientDetailsProperties.getClientDetials(type);
        ValidUtils.assertNotNull(clientDetials, "请使用正确方式来访问！");

        String url = String.format(AuthConstants.REFRESH_TOKEN_URL,refreshToken,
                clientDetials.getClientId(),clientDetials.getClientSecret());

        return HttpUtil.sendPost(url);
    }
}
